<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload extends CI_Controller {
	public function __construct()
    {
        parent::__construct();
    }

	public function index()
	{
		$config['upload_path']      = './uploads/';
        $config['allowed_types']    = 'jpg|png|jpeg';
        $config['max_size']     = 10000000000;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;
        var_dump($this->input->post());

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('upload_file'))
        {
            $error = array('error' => $this->upload->display_errors());
            var_dump($error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            var_dump($data);
        }
	}
	public function image_product_shows()
	{
		$config['upload_path']      = './uploads/ueditor/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['encrypt_name']  =  true;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('upload_file'))
        {
            $this->output->set_content_type('application/json')
                 ->set_output(json_encode(array('statu' => 'failed','message'=>$this->upload->display_errors())));
            return;
        }
        else
        {
            $data = $this->upload->data();
            $path = $config['upload_path'].$data['file_name'];
            if($data["file_size"]>1024){
                $this->output->set_content_type('application/json')
                 ->set_output(json_encode(array('statu' => 'failed','message'=>'请上传小于1M的图片！')));
                return;
            }
            $this->output->set_content_type('application/json')
                 ->set_output(json_encode(array('statu' => 'ok','message'=>'上传成功！','result'=>[
                    "path"=>substr($path,1),
                    "size"=>$data["file_size"],
                    "width"=>$data["image_width"],
                    "height"=>$data["image_height"]
                 ])));
            return;
        }
	}

    public function pass(){
        header('Access-Control-Allow-Origin:'.rtrim(base_url(), '/'));
        echo 'ok';
    }

    private function tosize($size) { 
        $units = array(' B', ' KB', ' MB', ' GB', ' TB'); 
        for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024; 
        return round($size, 2).$units[$i]; 
    }

    private function creatThum($img,$width,$height) { 
        $config['image_library'] = 'gd2';
        $config['source_image'] = $img;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = $width;
        $config['height']   = $height;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        return $this->image_lib->display_errors();
    }

}